package com.android.aircondition.common;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.aircondition.R;
import com.android.aircondition.constant.AppConstant;
import com.android.aircondition.service.GreeService;
import com.android.aircondition.service.Timer;

public class TimerListAdapter extends BaseAdapter{
	
	private Activity activity;
	private Timer timer;
	private GreeService service;
	private boolean clickKey;
	
	
	public TimerListAdapter(Activity activity) {
		this.activity = activity;
		service = new GreeService(activity);
		timer = service.getTimerPreference();
	}

	
	public int getCount() {
	
		return AppConstant.NO_2;
	}

	public Object getItem(int position) {

		return position;
	}

	public long getItemId(int position) {

		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		
		View view = LayoutInflater.from(activity).inflate(R.layout.timer_list_item, null);
		
		TextView titleTextView = (TextView)view.findViewById(R.id.timerItemTitle);
		final TextView switchOnTextView = (TextView)view.findViewById(R.id.timerSwitchOnTextView);
		final TextView switchOffTextView = (TextView)view.findViewById(R.id.timerSwitchOffTextView);
		final LinearLayout timerSwitchLinear = (LinearLayout)view.findViewById(R.id.timerSwitchLinear);
		
		
		switch (position) {
		
		case AppConstant.NO_0:
			
			titleTextView.setText(R.string.timer_item_title_powerOn);
			if(timer.isPowerOn())
				timerSwitchLinear.setBackgroundResource(R.drawable.swith_on);
			else
				timerSwitchLinear.setBackgroundResource(R.drawable.swith_off);
			
			OnClickListener powerOnListener = new OnClickListener() {
				
				public void onClick(View v) {
					if(clickKey) return;
					
					clickKey = true;
					
					switch (v.getId()) {
					case R.id.timerSwitchOnTextView:
						timerSwitchLinear.setBackgroundResource(R.drawable.swith_on);
						timer.setPowerOn(true);
						break;
					case R.id.timerSwitchOffTextView:
						timerSwitchLinear.setBackgroundResource(R.drawable.swith_off);
						timer.setPowerOn(false);
						break;
					}

					service.setTimerPreference(timer);
					clickKey = false;
					
				}
			};
			
			switchOnTextView.setOnClickListener(powerOnListener);
			switchOffTextView.setOnClickListener(powerOnListener);
			
			break;
		case AppConstant.NO_1:
			titleTextView.setText(R.string.timer_item_title_powerOff);
			if(timer.isPowerOff())
				timerSwitchLinear.setBackgroundResource(R.drawable.swith_on);
			else
				timerSwitchLinear.setBackgroundResource(R.drawable.swith_off);
			
			OnClickListener powerOffListener = new OnClickListener() {
				
				public void onClick(View v) {

					if(clickKey) return;

					clickKey = true;

					switch (v.getId()) {
					case R.id.timerSwitchOnTextView:
						timerSwitchLinear.setBackgroundResource(R.drawable.swith_on);
						timer.setPowerOff(true);
						break;

					case R.id.timerSwitchOffTextView:
						timerSwitchLinear.setBackgroundResource(R.drawable.swith_off);
						timer.setPowerOff(false);
						break;
					}

					service.setTimerPreference(timer);
					clickKey = false;
				}
			};
			
			switchOnTextView.setOnClickListener(powerOffListener);
			switchOffTextView.setOnClickListener(powerOffListener);
			
			break;

		}
		
		return view;
	}

}
